package mn.more.mock.io.xml;

import java.io.IOException;
import junit.framework.TestCase;
import mn.more.foundation.io.FileOperator;
import mn.more.foundation.io.FileUtil;
import mn.more.mock.io.MockModel;

/**
 * Created by IntelliJ IDEA.
 * User: Buyaa
 * Date: Jun 25, 2008
 * Time: 2:05:49 PM
 * To change this template use File | Settings | File Templates.
 *
 * @version $Id: ReaderTestArray.java 5 2008-09-01 12:08:42Z mikeliucc $
 */
public class ReaderTestArray extends TestCase {

	public static final char DELIM = (char) 254;
	private String filename = ReaderTestArray.class.getSimpleName() + "-fixture.xml";
	private String fixture = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
	                         "<!DOCTYPE models SYSTEM \"model.dtd\">\n" +
	                         "<models>\n" +
	                         "  <model id=\"user2\">\n" +
	                         "    <property name=\"userId\" type=\"String\"><![CDATA[&#009;]]></property>\n" +
	                         "    <property name=\"password\" type=\"String\"><![CDATA[Mohammed&#15;]]></property>\n" +
	                         "  </model>\n" +
	                         "  <model id=\"user1\">\n" +
	                         "    <property name=\"userId\" type=\"String\"><![CDATA[&nbsp;&nbsp;&nbsp;]]></property>\n" +
	                         "    <property name=\"password\" type=\"String\"><![CDATA[du\u0081dW[\u0085]]></property>\n" +
	                         "  </model>\n" +
	                         "  <model id=\"user4\">\n" +
	                         "    <property name=\"userId\" type=\"String\"><![CDATA[Gu]]></property>\n" +
	                         "    <property name=\"password\" type=\"String\"><![CDATA[Lucas&#4;&#1;]]></property>\n" +
	                         "    <property name=\"myArray\" type=\"StringArray\"><![CDATA[hello" +
	                         DELIM +
	                         "hi" +
	                         DELIM +
	                         "bye" +
	                         DELIM +
	                         "goodbye]]></property>\n" +
	                         "    <property name=\"myIntArray\" type=\"intArray\"><![CDATA[10" +
	                         DELIM +
	                         "100" +
	                         DELIM +
	                         "89" +
	                         DELIM +
	                         "11]]></property>\n" +
	                         "    <property name=\"myDoubleArray\" type=\"doubleArray\"><![CDATA[10.33" +
	                         DELIM +
	                         "100.898" +
	                         DELIM +
	                         "-4566.22]]></property>\n" +
	                         "  </model>\n" +
	                         "  <model id=\"user3\">\n" +
	                         "    <property name=\"userId\" type=\"String\"><![CDATA[G]]></property>\n" +
	                         "    <property name=\"password\" type=\"String\"><![CDATA[&#8;&#14;&#14&#]]></property>\n" +
	                         "  </model>\n" +
	                         "  <model id=\"user5\">\n" +
	                         "    <property name=\"userId\" type=\"String\"><![CDATA[GuI]]></property>\n" +
	                         "    <property name=\"password\" type=\"String\"><![CDATA[Dylan&#23;&#5;]]></property>\n" +
	                         "  </model>\n" +
	                         "  <model id=\"_all\">\n" +
	                         "    <property name=\"user2\" refid=\"user2\" />\n" +
	                         "    <property name=\"user1\" refid=\"user1\" />\n" +
	                         "    <property name=\"user4\" refid=\"user4\" />\n" +
	                         "    <property name=\"user3\" refid=\"user3\" />\n" +
	                         "    <property name=\"user5\" refid=\"user5\" />\n" +
	                         "  </model>\n" +
	                         "</models>\n";
	private static final String USER1 = "user1";
	private static final String USER_ID = "userId";
	private static final String PASSWORD = "password";
	private static final String MY_ARRAY = "myArray";
	private static final String INT_ARRAY = "myIntArray";
	private static final String DOUBLE_ARRAY = "myDoubleArray";

	protected void setUp() throws Exception {
		super.setUp();
		FileUtil.writeTextToFile(filename, fixture);
	}

	public void testReadArray() {
		XMLReader reader = new XMLReader();
		try {
			reader.setFilename(filename, false);
			reader.read();
		} catch (IOException e) {
			fail(e.getMessage());
		}

		MockModel model = reader.getModel(USER1);
		assertEquals(model.getId(), USER1);
		assertEquals(model.getString(USER_ID), "   ");
		assertEquals(model.getString(PASSWORD), "du\u0081dW[\u0085");

		model = reader.getModel("user2");
		assertEquals(model.getId(), "user2");
		assertEquals(model.getString(USER_ID), "\t");
		assertEquals(model.getString(PASSWORD), "Mohammed" + (char) 15);

		model = reader.getModel("user3");
		assertEquals(model.getId(), "user3");
		assertEquals(model.getString(USER_ID), "G");
		assertEquals(model.getString(PASSWORD), (char) 8 + "" + (char) 14 + "&#14&#");

		model = reader.getModel("user4");
		assertEquals(model.getId(), "user4");
		assertEquals(model.getString(USER_ID), "Gu");
		assertEquals(model.getString(PASSWORD), "Lucas" + (char) 4 + (char) 1);

		assertTrue(model.getStringArray(MY_ARRAY).length == 4);
		assertTrue(model.getStringArray(MY_ARRAY)[2].equals("bye"));

		assertEquals(model.getIntArray(INT_ARRAY)[0], 10);
		assertEquals(model.getIntArray(INT_ARRAY)[1], 100);
		assertEquals(model.getIntArray(INT_ARRAY)[2], 89);
		assertEquals(model.getIntArray(INT_ARRAY)[3], 11);

		assertEquals(model.getDoubleArray(DOUBLE_ARRAY)[0], 10.33);
		assertEquals(model.getDoubleArray(DOUBLE_ARRAY)[1], 100.898);
		assertEquals(model.getDoubleArray(DOUBLE_ARRAY)[2], -4566.22);

		model = reader.getModel("user5");
		assertEquals(model.getId(), "user5");
		assertEquals(model.getString(USER_ID), "GuI");
		assertEquals(model.getString(PASSWORD), "Dylan" + (char) 23 + (char) +5);
	}

	protected void tearDown() throws Exception {
		FileOperator.delete(filename);
		super.tearDown();
	}
}
